package CutLoad.components.loadbar
{
	import CutLoad.Base;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * 这是类的说明
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 下午02:00:55 
	 *
	 */
	public class McLoadBarMustPlayOnce extends Base implements ILoadBar
	{
		protected var mc:MovieClip;
		protected var mcBar:MovieClip;
		protected var mcLinkName:String;
		protected var stopFrame:Number;
		protected var mcLoadBarName:String;
		protected var currentMcPlayFrame:Number;
		protected var currentHaveLoad:Number;
		protected var loadCompeleteDo:Function;
		public function McLoadBarMustPlayOnce(mcLinkName:String,stopFrame:Number,mcLoadBarName:String="mcbar")
		{
			this.mcLinkName=mcLinkName;
			this.mc=Base.getMcFromStrClass(mcLinkName);
			if(mc[mcLoadBarName]==null) throw new Error("库元件："+mcLinkName+" 里不存在:"+mcLoadBarName);
			if(mc[mcLoadBarName].totalFrames!=100) throw new Error("库元件："+mcLinkName+" 里的:"+mcLoadBarName+" 帧数必须是100");
			if(mc.totalFrames<=stopFrame) throw new Error("库元件："+mcLinkName+" 帧数不能小于或等行设置的stopFrame值");
			this.stopFrame=stopFrame;
			this.mcLoadBarName=mcLoadBarName;
			currentMcPlayFrame=1;
			currentHaveLoad=0;
			//
			this.addChild(mc);
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			//最后一帧加上代码
			mc.addFrameScript(mc.totalFrames-1,frameEnd);
			mc.play();
			mc[mcLoadBarName].gotoAndStop(1);
			mcBar=mc[mcLoadBarName];
		}
		private function enterFrame(e:Event):void{
			if(mc.currentFrame==stopFrame){
				if(currentMcPlayFrame<100) mc.stop();
			}
			if(currentMcPlayFrame<currentHaveLoad){
				currentMcPlayFrame++;
			}
			if(currentMcPlayFrame==100){
				currentMcPlayFrame=100;
				mc[mcLoadBarName].gotoAndStop(currentMcPlayFrame);
				mc.play();
				if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
				return;
			}
			mc[mcLoadBarName].gotoAndStop(currentMcPlayFrame);
		}
		private function frameEnd():void{
			mc.gotoAndStop(1);
			mc[mcLoadBarName].gotoAndStop(1);
			//
			if(loadCompeleteDo!=null) loadCompeleteDo();
			if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			currentMcPlayFrame=1;
		}
		public function dreawing():void
		{
			
		}
		public function setValue(per:Number, strTalkYou:String):void
		{
			var pp:Number=Math.floor(per*100)
			currentHaveLoad=pp;
		}
		
		public function showHide(isShow:Boolean):void
		{
			this.visible=isShow;
			if(!isShow){
				if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
				currentMcPlayFrame=1;
			}
		}
		
		public function setLoadCompleteDo(fun:Function):void
		{
			this.loadCompeleteDo=fun;
			if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			this.addEventListener(Event.ENTER_FRAME,this.enterFrame);
			currentMcPlayFrame=1;
			mc.gotoAndStop(1);
			mc[mcLoadBarName].gotoAndStop(1);
		}
		public function isLoadCompleteDo():Boolean
		{
			return true;
		}
		
		public function loadComplete():void
		{
			currentHaveLoad=100;
		}
		
		public function clone():ILoadBar
		{
			return new McLoadBarMustPlayOnce(mcLinkName,stopFrame,mcLoadBarName);
		}
	}
}